package org.zhu.newDateUtil;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @author yingzhi zhu
 *         date 2018/1/12.
 *    java 8 新的 时间处理类练习
 */
public class Client {
    public static void main(String[] args) {
//        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault());
//        String format = localDateTime.format(DateTimeFormatter.ofPattern("yyyy:MM:dd HH:mm:ss"));
//        System.out.println(format);
//        LocalDate nowDate = LocalDate.parse("20180112", DateTimeFormatter.BASIC_ISO_DATE);
        System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        System.out.println(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        getBeforeDayFormatToMap("20180117","yyyyMMdd",10);
    }

    //获取指定天数之前的指定的日期长度
    public static List<String> getBeforeDayFormat(String currenDay, String format, int day){
        LocalDate localDate = LocalDate.parse(currenDay, DateTimeFormatter.ofPattern(format));
        return IntStream.range(0,day).
                mapToObj(i-> localDate.minusDays(i).format(DateTimeFormatter.ofPattern(format))).collect(Collectors.toList());
    }

    //获取指定天数之前的指定的日期长度
    public static Map<String,Long> getBeforeDayFormatToMap(String currenDay, String format, int day){
        LocalDate localDate = LocalDate.parse(currenDay, DateTimeFormatter.ofPattern(format));
        IntStream.range(0,day).
                mapToObj(localDate::minusDays).
                collect(Collectors.toMap(
                key-> key.format(DateTimeFormatter.ofPattern(format)),
                value-> value.atStartOfDay().toInstant(ZoneOffset.of("+8")).toEpochMilli())).forEach((key,value)->{
            System.out.println(key);
            System.out.println(value);
        });
        return null;
    }
}
